package com.peaker.advertisingpro.activity

import android.content.Intent
import android.os.Bundle
import android.text.TextUtils
import android.view.View
import com.peaker.advertisingpro.R
import com.peaker.advertisingpro.base.BaseActivity
import com.peaker.advertisingpro.db.AppDatabase
import com.peaker.advertisingpro.net.TcpClient
import com.peaker.advertisingpro.tcp.TCPClient
import com.peaker.advertisingpro.utils.*
import com.peaker.advertisingpro.view.MsgDialog
import com.peaker.advertisingpro.view.PwdDialog
import kotlinx.android.synthetic.main.setting_layout.*
import java.io.File

class SysSetActivity : BaseActivity(), View.OnClickListener {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.setting_layout)

        btn_back.setOnClickListener(this)
        btn_name_setting.setOnClickListener(this)
        btn_onoff_setting.setOnClickListener(this)
        btn_volume_setting.setOnClickListener(this)
        btn_start_setting.setOnClickListener(this)
        btn_exit_app_pwd_set.setOnClickListener(this)
        btn_clean_log.setOnClickListener(this)
        btn_screen_rotation_setting.setOnClickListener(this)
        btn_reset_setting.setOnClickListener(this)
        btn_onoffline_setting.setOnClickListener(this)

        setText()
    }

    private fun setText() {
        var name =
            if (TextUtils.isEmpty(SPUtil.getSP(Common.MACNAME))) SmdtManagerUtils.smdtGetEthMacAddress() else SPUtil.getSP(
                Common.MACNAME
            )
        var on = if (TextUtils.isEmpty(SPUtil.getSP(Common.BOOTTIME))) "无" else SPUtil.getSP(Common.BOOTTIME)
        var off = if (TextUtils.isEmpty(SPUtil.getSP(Common.SHUTDOWN))) "无" else SPUtil.getSP(Common.SHUTDOWN)
        var reboot = if (TextUtils.isEmpty(SPUtil.getSP(Common.REBOOT))) "关" else SPUtil.getSP(Common.REBOOT)
        var protect = if (TextUtils.isEmpty(SPUtil.getSP(Common.PROTECT))) "关" else SPUtil.getSP(Common.PROTECT)
        var moudle = if (TextUtils.isEmpty(SPUtil.getSP(Common.MOUDLE))) "联机模式" else SPUtil.getSP(Common.MOUDLE)
        var angle = if (TextUtils.isEmpty(SPUtil.getSP(Common.ANGLE))) "0°" else SPUtil.getSP(Common.ANGLE)

        tv_name_setting.text = "终端名称: $name"
        tv_on_setting.text = "开机时间: $on"
        tv_off_setting.text = "关机时间: $off"
        tv_volume_current_setting.text = "当前音量: ${SmdtManagerUtils.getCurrentSysVolume()}"
        tv_max_volume_setting.text = "最大音量: 100"
        tv_start_reboot_setting.text = "开机自启: $reboot"
        tv_start_protect_setting.text = "应用守护: $protect"
        tv_onoffline_current_setting.text = "当前模式: $moudle"
        tv_screen1_setting.text = "屏幕旋转角度: $angle"

        if (moudle == "联机模式") {
            btn_onoff_setting.isEnabled=false
            btn_screen_rotation_setting.isEnabled=false
            SPUtil.putSP(Common.ISCONNECT, "否")//这样返回首页会重联

        } else {
            btn_onoff_setting.isEnabled=true
            btn_screen_rotation_setting.isEnabled=true
            Common.LOGINGFLAG=1
            TCPClient.build().closeTcp()
        }

    }

    override fun onClick(v: View?) {
        when (v?.id) {
            R.id.btn_back -> {
                this.finish()
            }
            R.id.btn_name_setting -> {
                var intent = Intent(this, ActionActivity::class.java)
                intent.putExtra("layout", R.layout.layout_setname)
                startActivityForResult(intent,1001)
            }
            R.id.btn_onoff_setting -> {//开关机设置
                val intent = Intent(this, ActionActivity::class.java)
                intent.putExtra("layout", R.layout.dialog_plan_layout_new)
                startActivityForResult(intent,1001)
            }
            R.id.btn_onoffline_setting -> {
                val intent = Intent(this, ActionActivity::class.java)
                intent.putExtra("layout", R.layout.dialog_system_layout)
                startActivityForResult(intent,1001)

            }
            R.id.btn_volume_setting -> {
                val intent = Intent(this, ActionActivity::class.java)
                intent.putExtra("layout", R.layout.dialog_volume_layout)
                startActivityForResult(intent,1001)


            }
            R.id.btn_start_setting -> {
                val intent = Intent(this, ActionActivity::class.java)
                intent.putExtra("layout", R.layout.dialog_boot_layout)
                startActivityForResult(intent,1001)

            }
            R.id.btn_exit_app_pwd_set -> {//设置密码
                if (TextUtils.isEmpty(SPUtil.getSP(Common.PWD)).not()) {
                    var pwdDialog = PwdDialog(this)
                    pwdDialog.show()
                    pwdDialog.onPwdSucced = {
                        if (it == 0) {
                            val intent = Intent(this, ActionActivity::class.java)
                            intent.putExtra("layout", R.layout.layout_setpwd)
                            startActivity(intent)
                        } else {
                            ToastUtils.s("密码错误")
                        }
                    }
                } else {
                    val intent = Intent(this, ActionActivity::class.java)
                    intent.putExtra("layout", R.layout.layout_setpwd)
                    startActivity(intent)
                }

            }
            R.id.btn_clean_log -> {

                ToastUtils.s("清除日志成功")
            }
            R.id.btn_screen_rotation_setting -> {
                //屏幕旋转
                val intent = Intent(this, ActionActivity::class.java)
                intent.putExtra("layout", R.layout.dialog_screen_rotation_layout)
                startActivityForResult(intent,1001)

            }
            R.id.btn_reset_setting -> {
                //定时开关机清除

                var msgDialog = MsgDialog(this)
                msgDialog.show()
                msgDialog.onDialogSucced={
                    if(it==0){
                        AppDatabase.instance.programUrlDao().deletAll()
                        AppDatabase.instance.programJsonDao().deletAll()
                        AppDatabase.instance.volumeDao().deletAll()
                        AppDatabase.instance.interPlayDao().deletAll()
                        ZipUtils.deleteDirectory(Common.DIRADDRESS ,false)
                        (ScrollActivity.scrollAsc as ScrollActivity).setScreenonChange("0")
                        (ScrollActivity.scrollAsc as ScrollActivity).fomatJson()
                        SPUtil.removeAll()
                        TCPClient.build().closeTcp()
                        setText()
                        ToastUtils.s("初始化成功")
                    }

                }


            }
        }

    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        setText()

    }
}
